package com.runa.rccprotocol.analysis;

import com.runa.protocol.analysis.MetaBufferAnalysis;
import com.runa.protocol.analysis.ShellBufferAnalysis;
import com.runa.protocol.dto.DtuReport;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.AffirmDeny;
import com.runa.rccprotocol.dto.ConcentratorElectric;
import com.runa.rccprotocol.dto.equdata.ConcenReportData;
import com.runa.rccprotocol.dto.huanxiang.HuanXiangReportAck;
import com.runa.rccprotocol.dto.runa.RunaConcenAffirmDeny;

import java.util.Date;

/**
 * 环翔集中器
 * HX_FMU
 *
 * @author diandian
 * @Date 2022/4/29 10:22
 */
public class HuanXiangFmuAnalysis extends ShellBufferAnalysis<byte[]> {

    public HuanXiangFmuAnalysis(MetaBufferAnalysis analysis) {
        super(analysis, 400);
    }

    @Override
    public int fill(byte[] data, int length,String n) {
        this.interData = null;
        int begin = -1;
        for (int i = 0; i < data.length; i++) {
            if ((data[i] & 0xff) == 0xC0) {
                begin = i;
                break;
            }
        }

        int index = begin;

        int typeCode = data[++index] & 0xff;

        if (typeCode == 0x21) {
            //注册、心跳 报文
            return registry(data, length, begin, index);
        } else if (typeCode == 0x32) {
            //报文长度校验
            Short dataLen = ByteUtil.byteToShort(data, begin + 10, ByteOrder.AB);
            int surplus = length - (begin + dataLen + 14);
            if (surplus < 0) {
                return -1;
            }
            //判断指令是否处理成功，处理失败直接返回
            int ctrCode = data[begin + 9] & 0xff;
            // 1 命令处理失败，2 Mbus过载 ，3 Mbus忙碌
            if (ctrCode == 0x01 || ctrCode == 0x02 || ctrCode == 0x03) {
                AffirmDeny affirmDeny = new RunaConcenAffirmDeny(netEquNo,false);
                this.interData = affirmDeny;
                return surplus;
            }

            int equDataLen = dataLen - 6;
            System.arraycopy(data, 18, buffer, 0, equDataLen);

            int fill = this.analysis.fill(this.buffer, equDataLen,netEquNo);

            this.interData = this.analysis.gain();
            return surplus;
        }

        return 0;
    }

    private int registry(byte[] data, int length, int begin, int index) {
        //报文长度
        if (length < (begin + 14)) {
            return -1;
        }
        //功能码
        if (0x01 != (data[begin + 8] & 0xff)) {
            return length - (begin + 8);
        }
        Short dataLen = ByteUtil.byteToShort(data, begin + 10, ByteOrder.AB);
        if (length < (begin + dataLen + 14)) {
            return -1;
        }

        int allLen = (begin + 14 + dataLen);

        //校验
        byte check = data[begin+allLen-2];
        if (check != ByteUtil.sum(data, begin+1, allLen-3)) {
            return length - allLen;
        }
        if (netEquNo == null) {
            this.netEquNo = ByteUtil.byteToHexString(data, begin+2, 6);
            DtuReport dtuReport = new DtuReport(netEquNo);
            dtuReport.setMustAck(true);
            dtuReport.setAck(new HuanXiangReportAck(netEquNo));
            this.interData = dtuReport;
        } else {
            //因为心跳注册都一样，且注册包携带了信号的值，所以要区分注册和心跳包，在心跳包的时候解析信号，因为框架设计只能在心跳的时候解析
            if (dataLen == 1) {
                ConcentratorElectric dtuReport = new ConcentratorElectric(netEquNo);
                ConcenReportData reportData = new ConcenReportData();
                reportData.setTime(new Date());
                reportData.setDate(new Date());
                byte datum = data[begin + 12];
                reportData.setCsq((int) datum);
                dtuReport.setReportData(reportData);
                dtuReport.setMustAck(true);
                dtuReport.setAck(new HuanXiangReportAck(netEquNo));
                this.interData = dtuReport;
            }
        }

        return length - allLen;
    }

    public static void main(String[] args) {
//        String registry = "C0210300402206350100000115D8D0";
//        String meterRes = "C03203004022063502000057C71C0A028F1F02034C000000000B3B000000000B2C000000000C14000000000C06000000000C060A4A00000A4200000004000000000000000000000000000000000000000000000000FF02FFFF0002000100000000C5387DD0";
//        String meterRes = "C03203004022063502000057C71C0A028F1F02034C000000000B3B000000000B2C000000000C14000000000C06000000000C060AD400000ABF00000004000000000000000000000000000000000000000000000000FF02FFFF0002000100000000B80D4CD0";
//        String meterRes = "C03203004022063502000057C71C0A028F1F02034C000000000B3B000000000B2C000000000C14000000000C06000000000C060AD500000AC500000004000000000000000000000000000000000000000000000000FF02FFFF000200010000000004BE50D0";
        String meterRes = "C03203004022063502000057C71C0A028F1F02034C000000000B3B000000000B2C000000000C14000000000C06000000000C060A8700000A7900000004000000000000000000000000000000000000000000000000FF02FFFF00020001000000008D9D1ED0";
        String meterRes2 = "C03203004021505402000057C0A8049F8AEA05034C000000000B3B000000000B2C2CEA00020C1434A400000C06000000000C060BE900000C1600000000085E000000000000000000000000000000000000000000000005000000050001000000002F6431D0";
        String meterRes3 = "C03203004020200802010006C0A8049F8AEA45D0";
        String valveRes = "C03203004022063502000026C71C0A028F1FFEFEFE68466313000000000081101F9001000000000000E03800E038000BA016E7D0";
        String valveRes2 = "C03203004020201002000025C0A8049F8AEAFEFEFE684693080100000081101F90010000000000003747021447020068164BD0";
        String valveRes3 = "C03203004020201002000026C0A8049F8AEAFEFEFE68469308010000000081101F90010000000000003C470219470200721660D0";

        //返回的长度不对数据
//        String buildValve = "C03203004021505502000057C71C0A028F1F44034C000000000B3B000000000B2CAAF800010C14241200000C06000000000C060C5F00000C8B00000000064200000000000000000000000000000000000000000000004400000068000100000000A12117D0";

        //正确解析的数据
//        String buildValve = "C03203004021506102000057C0A8049F8AEADE034C000000000B3B000000000B2C000D00000C14000500000C06000000000C060C6700000C820000000000000000000000000000000000000000000000000000000002DE00000222000100000000084572D0";


        //控制码错误的
        String buildValve = "C0320300402107610200003BC0A8049F8AEAFEFEFE68462549000000000081252190010000000000002C36052C36050B00FF000000000000000000000000000000000000004C1662D0";



//        String register = "C021030040215054010000401738363530313630353732383731393534363030343836343539303435353638393836303436363132313943313833323635361B167D58EA8A0302150C08060085D0";
        byte[] bytes = ByteUtil.hexStringToByte(buildValve);
        HuanXiangFmuAnalysis analysis = new HuanXiangFmuAnalysis(new HuanXiangEquAnalysis());
        int fill = analysis.fill(bytes, bytes.length,"");

    }
}